package com.zy.list.list

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.zy.list.ListConstant.ARG_PARENT_ID
import com.zy.list.R
import com.zy.list.create.CreateNewFragment
import com.zy.list.placeholder.PlaceholderContent
import com.zy.storage.DataBaseUtils
import com.zy.zlog.ZLog

/**
 * A fragment representing a list of Items.
 */
class DocItemFragment : Fragment() {

    private var columnCount = 1
    private var parentId = 0L
    private var mAdapter: MyDocItemRecyclerViewAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        arguments?.let {
            columnCount = it.getInt(ARG_COLUMN_COUNT)
            parentId = it.getLong(ARG_PARENT_ID)
        }

        ZLog.d(TAG, "parentId= $parentId")
        //监听数据变化,返回数据库后的内容
        //2023-09-16 21:23:17.561 15497-15497 ZYDebug                 com.zy.world                         D  DocItemFragment []
        //2023-09-16 21:23:18.819 15497-15497 ZYDebug                 com.zy.world                         D  DocItemFragment click create new
        //2023-09-16 21:23:19.874 15497-15497 ZYDebug                 com.zy.world                         D  DocItemFragment [DocInfo(uid=1, title=文件, des=文件, createTime=1694870599848, updateTime=1694870599848, fileType=1)]
        //2023-09-16 21:23:26.533 15497-15497 ZYDebug                 com.zy.world                         D  DocItemFragment click create new
        //2023-09-16 21:23:27.622 15497-15497 ZYDebug                 com.zy.world                         D  DocItemFragment [DocInfo(uid=1, title=文件, des=文件, createTime=1694870599848, updateTime=1694870599848, fileType=1), DocInfo(uid=2, title=文件夹, des=文件夹, createTime=1694870607599, updateTime=1694870607599, fileType=0)]
        //2023-09-16 21:23:43.939 15497-15497 ZYDebug                 com.zy.world                         D  DocItemFragment click create new
        //2023-09-16 21:23:44.733 15497-15497 ZYDebug                 com.zy.world                         D  DocItemFragment [DocInfo(uid=1, title=文件, des=文件, createTime=1694870599848, updateTime=1694870599848, fileType=1), DocInfo(uid=2, title=文件夹, des=文件夹, createTime=1694870607599, updateTime=1694870607599, fileType=0), DocInfo(uid=3, title=文件, des=文件, createTime=1694870624701, updateTime=1694870624701, fileType=1)]
        DataBaseUtils.getDocInfoDao().getAllLiveData(parentId).observe(this) {
            ZLog.d(TAG, "" + it)
            PlaceholderContent.ITEMS.clear()
            PlaceholderContent.ITEMS.addAll(it.reversed())

            mAdapter?.notifyDataSetChanged()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_item_main_list, container, false)
        val rv: RecyclerView = view.findViewById(R.id.main_list)
        // Set the adapter
        with(rv) {
            layoutManager = when {
                columnCount <= 1 -> LinearLayoutManager(context)
                else -> GridLayoutManager(context, columnCount)
            }
            mAdapter = MyDocItemRecyclerViewAdapter(PlaceholderContent.ITEMS, parentFragmentManager)
            adapter = mAdapter
        }

        val createNewButton: View = view.findViewById(R.id.create_new)
        createNewButton.setOnClickListener {
            ZLog.d("DocItemFragment", "click create new")
            CreateNewFragment.show(parentFragmentManager, parentId)

            //CreateOrModifyTitleFragment.show(parentFragmentManager, 1)
        }

        return view
    }

    companion object {
        const val TAG = "DocItemFragment"

        const val ARG_COLUMN_COUNT = "column-count"

        @JvmStatic
        fun newInstance(parentId: Long) = DocItemFragment().apply {
            arguments = Bundle().apply {
                putInt(ARG_COLUMN_COUNT, 1)
                putLong(ARG_PARENT_ID, parentId)
            }
        }
    }
}